import { queryInstDeatilInfo, queryInstOrderInfo } from '../../services/inspect-project'
import { queryOrderById } from '../../services/order'
import { queryVehicleDetail } from '../../services/vehicle'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    instId:"",
    inspectList:[],
    inspectItem:{},
    token:"",
    basicInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }
    this.getToken()

    let instId = options.instId
    let orderId = options.orderId
    this.getInstDeatilInfo(instId)
    this.getOrderDetailInfo(orderId)
    this.setData({
      ['basicInfo.reportId']: instId,
      ['basicInfo.orderId']: orderId
    })
    
  },

  onShow(){
    this.getToken()
  },

  getToken(){
    this.setData({
      token: wx.getStorageSync('token')
    })
  },

  dealJson(dataList){
    let tempDataList = dataList
    tempDataList.forEach(item => {
      if(item.itemJson && typeof item.itemJson == 'string'){
        item.itemJson = JSON.parse(item.itemJson)
      }
    })
    return tempDataList
  },

  async getInstDeatilInfo(instId){
    let res = await queryInstDeatilInfo(instId)
    console.log("res>>>", res)
    if(res.code === 200){
      let inspectItem = res.data
      let inspectList = this.dealJson(JSON.parse(inspectItem.itemJson)) 
      this.setData({
        inspectItem,
        inspectList,
        ['basicInfo.reportDate']: inspectItem.updateTime,
        ['basicInfo.detectStartTime']: inspectItem.detectStartTime || "",
        ['basicInfo.detectEndTime']: inspectItem.detectEndTime || "",
      })
    }
  },

  async getOrderDetailInfo(orderId){
    let orderRes = await queryOrderById(orderId)
    console.log("orderRes>>>>", orderRes)
    if(orderRes.code === 200){
      let orderInfo = orderRes.data
      let { vehicleId, plateNo } = orderInfo
      this.setData({
        orderInfo,
        ['basicInfo.plateNo']: plateNo || ""
      })
      if(vehicleId){
        this.getVehicleDetailInfo(vehicleId)
      }
    }
  },

  async getVehicleDetailInfo(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    if(res.code === 200){
      let vehicleInfo = res.data
      this.setData({
        vehicleInfo,
        ['basicInfo.vin']: vehicleInfo.vin || ""
      })
    }
  }

  // async getInstInfoList(orderId){
  //   let res = await queryInstOrderInfo({ orderId })
  //   console.log("order>>>res>>>", res)
  //   if(res.code === 200){
  //     let inspectList = res.rows
  //     this.setData({
  //       inspectList
  //     })
  //   }
  // }



})